from base64 import b64decode
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5 as PKCS1_cipher

MAX_DECRYPT_BLOCK = 128  # 对于 RSA, 一般取决于密钥长度


def decrypt_by_private_key(encrypted_text, private_key_str):
    # 从字符串中加载私钥
    private_key = RSA.import_key(private_key_str)

    # 使用私钥创建解密器
    cipher_rsa = PKCS1_cipher.new(private_key)

    # Base64 解码加密数据
    encrypted_data = b64decode(encrypted_text)

    # 对数据分段解密
    decrypted_data = bytearray()
    input_len = len(encrypted_data)
    offset = 0

    while input_len - offset > 0:
        if input_len - offset > MAX_DECRYPT_BLOCK:
            chunk = encrypted_data[offset:offset + MAX_DECRYPT_BLOCK]
            decrypted_chunk = cipher_rsa.decrypt(chunk, None)
        else:
            chunk = encrypted_data[offset:]
            decrypted_chunk = cipher_rsa.decrypt(chunk, None)

        decrypted_data.extend(decrypted_chunk)
        offset += MAX_DECRYPT_BLOCK

    return decrypted_data.decode('utf-8')


# 假设这是你的私钥文本
private_key_str = """-----BEGIN RSA PRIVATE KEY-----
MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIn2zWqU7K/2qm5pOpq5bp9R+3MTnStWTfJU9nC/Vo7UKH9dITPvrELCTK+qlqpx5Fes+l0GY7n6u4n4jyiw4ejsvkZYQ5ww477yLOn2FcoEGuZEwPgSCmfTST0OFUgQqn+/J11k9L92jEHyieE3qmhMkMt0UsVUSJwx/nZxo30ZAgMBAAECgYBD3YHigeuEC4R+14iaf8jo2j0kuGtB3Cxvnlez0otTqw1YyYkBsU49cLKkXvfKVEgM0Ow/QltgKvSBxCE31PrrDka5TygVMqqA/IM7NrDvjUcGLjyoeNmLA8660fWcDxUTlAGN5kxIvUATayVwKVflpWPWu0FPKsWrZustnEo+4QJBAMCmYsWqAKWYMVRXFP3/XGRfio8DV793TOckyBSN9eh8UhgoZyT3u7oeHmDJEwm4aNMHlg1Pcdc6tNsvi1FRCiUCQQC3VNzfF4xOtUgX7vWPL8YVljLuXmy12iVYmg6ofu9l31nwM9FLQ1TRFglvF5LWrIXTQb07PgGd5DJMAQWGsqLlAkAPE7Z9M73TN+L8b8hDzJ1leZi1cpSGdoa9PEKwYR/SrxAZtefEm+LEQSEtf+8OfrEtetWCeyo0pvKKiOEFXytFAkEAgynL/DC0yXsZYUYtmYvshHU5ayFTVagFICbYZeSrEo+BoUDxdI9vl0fU6A5NmBlGhaZ65G+waG5jLc1tTrlvoQJAXBEoPcBNAosiZHQfYBwHqU6mJ9/ZacJh3MtJzGGebfEwJgtln5b154iANqNWXpySBLvkK+Boq7FYRiD83pqmUg==
-----END RSA PRIVATE KEY-----"""

# 假设这是加密后的 Base64 编码数据
encrypted_text = "UJnBiF0 + Bx8cMfskQMbmfXcFqIEer9VtekNUHE98Ai56SjoLjX2d0GL2nQQ4h0vQG2U8JSdVsI06wNC3CVCTfH0yTNEIit6EJDeQEffBxc / W5bySBZaKEa9WXQsp1ZMDKTNszhX1Einvq5N3WwtHDmuWpYOqOjQEs3VjohsoiBxaOoht8RC4nRVcBYtXED / +l + PlXOMRf + UkLKbr + KMo5NujZN5iWhlqyG61OiA / Ise8jFaDBVe5VO9e9DYODAWhidlTvZ //drGd9Co41PSqr5B59YlrTKsY/duCxDSHKnxKa1vN+bIXAMa43uhp4iBcu/OWlABhDF2gS5zq0EUP0laUwEDYRNWK8VBX/PDQunXW8In1Mr3bOtGtW/sljL/c1S11W8GTJMQOYzJNyYMF6q6HgfEjquFdM1TfI62upO2IhB14aDj4rEefb3CVMXlIxOaUEyd/eRBeo6qaYtRXsOYnWBmrrenexv06LOgqTq4TGY9UFfIcGm5af+WwAPZxCBzThJl1exFxlAObemHs6bwF+plzi88WYGEaN9f2ghweBcOG7GT5dg+WNH7aZ1a+p9U77mSbGVTYaW7ibfHngSnvj5c/kAcYFCFW25JMbLBZU3C+h4ciHMxPLKSkvXqP6wU/sNztm9FOmMIgO5dAva//9GnPsZby6aPYrdxlwU9WIFQuFexG8N6fqrILkX8pdeM5vPXodXAP8mHP6mBRa/cB2po1IWwc56qslEy3N5uabYodvJqicDfNWH6MqcT7Ltpyap9BAJizRwemaw2e/cgajhPkjPxRF4bV77d378n/Q7vRXh0u3gRxt6xh/ERz4lrxDDEPcNdG/JrmuMRNTw=="

try:
    decrypted_text = decrypt_by_private_key(encrypted_text, private_key_str)
    print("请求解密后的文本：")
    print(decrypted_text)
except Exception as e:
    print("请求解密失败：", e)
